package com.company.project.web;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.JdPhones;
import com.company.project.service.JdPhonesService;
import com.company.project.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
* Created by CodeGenerator on 2019/07/23.
*/
@RestController
@RequestMapping("/jd/phones")
public class JdPhonesController {
    @Resource
    private JdPhonesService jdPhonesService;

    @PostMapping("/add")
    public Result add(JdPhones jdPhones) {
		jdPhones.setId(StringUtil.getUUID());
        jdPhonesService.save(jdPhones);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        jdPhonesService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(JdPhones jdPhones) {
        jdPhonesService.update(jdPhones);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        JdPhones jdPhones = jdPhonesService.findById(id);
        return ResultGenerator.genSuccessResult(jdPhones);
    }

    @GetMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size)
			throws IOException {

		try {
			BufferedReader reader = new BufferedReader(new FileReader("/Users/72cy-0101-01-0009/Downloads/phones.csv"));//换成你的文件名
			reader.readLine();//第一行信息，为标题信息，不用,如果需要，注释掉
			String line = null;

			int i = 0;
			List<JdPhones> list = new ArrayList<>();

			while((line=reader.readLine())!=null){

				System.out.println("line --- " + line);

				String item[] = line.split(",");//

				String number = item[1];
				String type = item[2];
				String regionId = item[3];

				// if (Integer.valueOf(regionId) == 9 || Integer.valueOf(regionId) == 13) {
				// if (Integer.valueOf(regionId) == 15) {

					i ++;
					JdPhones jdPhones = new JdPhones();
					jdPhones.setId(StringUtil.getUUID());
					jdPhones.setNumber(Integer.valueOf(number));
					jdPhones.setType(Integer.valueOf(type));
					jdPhones.setRegionId(Integer.valueOf(regionId));

					// jdPhonesService.save(jdPhones);
					// System.out.println("----" + i);


					list.add(jdPhones);
//
//					if (i == 2000) {
//						System.out.println("+++++");
//						jdPhonesService.insertForeach(list);
//						i = 0;
//						list = new ArrayList<>();
//
//					}
//
//					if (line.equals("415284,1999979,3,254")) {
//						jdPhonesService.insertForeach(list);
//					}

				}


			// }

			System.out.println("total " + i);

		} catch (Exception e) {
			e.printStackTrace();
		}



//        PageHelper.startPage(page, size);
//        List<JdPhones> list = jdPhonesService.findAll();
//        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult("");
    }
}
